package org.crumbleworks.crumbchat.server.controller;

import javax.swing.JOptionPane;

import org.crumbleworks.crumbchat.common.data.Message;
import org.crumbleworks.crumbchat.server.gui.ChatFrame;

public class GuiController implements ServerControllerInterface {
    private ChatFrame chatFrame;
    private ServerControllerInterface controller;

    public GuiController(ServerControllerInterface controller) {
        this.controller = controller;

        chatFrame = new ChatFrame(this);
    }

    @Override
    public void bindPorts(int tcpPort, int udpPort) {
        controller.bindPorts(tcpPort, udpPort);
    }

    @Override
    public void unbindPorts(boolean shutdownAfterUnbind) {
        controller.unbindPorts(shutdownAfterUnbind);
    }

    @Override
    public void sendMessageToClients(String text) {
        controller.sendMessageToClients(text);
    }

    @Override
    public void putMessageToChat(Message message) {
        chatFrame.getChatPanel().putMessage(message.getUsername(), message.getMessage());
    }

    @Override
    public void showBindError(int tcpPort, int udpPort) {
        JOptionPane.showMessageDialog(chatFrame.getPortPanel(), "Konnte Ports nicht auf 'TCP: " + tcpPort + "' und/oder 'UDP: "
                + udpPort + "' binden.", "Keine Verbindung", JOptionPane.INFORMATION_MESSAGE);
    }

    @Override
    public void shutdown() {
        chatFrame.dispose();
    }

    @Override
    public void parseInput(String input) {
        controller.parseInput(input);
    }
}
